package com.tanger.im.service.record;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.tanger.im.IMUserRecordHelper;
import com.tanger.im.entity.IMQuery;
import com.tanger.im.entity.ImUserRecord;
import com.tanger.im.mapper.IMUserRecordRepository;
import com.tanger.im.service.members.IMUserGroupMembersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @Description TODO
 * @Author Hchenbin
 * @Date 2023-12-25 15:43
 * @Version 1.0
 */
@Slf4j
@Service
public class IMUserRecordServiceImpl implements IMUserRecordService {

    @Resource
    private IMUserRecordRepository repository;
    @Autowired
    private IMUserGroupMembersService membersService;

    @Override
    public List<ImUserRecord> findAll(Long userId) {
        List<Long> groupIds = membersService.findAllByUserId(userId);
        return Optional.ofNullable(repository.selectList(
                new QueryWrapper<ImUserRecord>()
                        .or()
                        .eq("fu_id", userId)
                        .eq("tu_id", userId)
                        .in(CollectionUtils.isEmpty(groupIds)? false : true,"fu_id", groupIds)))
                .orElseGet(Collections::emptyList);
    }

    @Override
    public JsonNode findOne(IMQuery query) {
        return IMUserRecordHelper.read(query);
    }

    @Override
    public JsonNode findGroup(IMQuery query) {
        return IMUserRecordHelper.read(query);
    }
}
